#ifndef __CH374_USB_H__
#define __CH374_USB_H__

#include <stdint.h>

/* ********************************************************************************************************************* */
/* USB��????? */

/* USB??????PID, ?????????????? */
#ifndef	DEF_USB_PID_SETUP
#define	DEF_USB_PID_NULL	0x00			/* ????PID, ��???? */
#define	DEF_USB_PID_SOF		0x05
#define	DEF_USB_PID_SETUP	0x0D
#define	DEF_USB_PID_IN		0x09
#define	DEF_USB_PID_OUT		0x01
#define	DEF_USB_PID_ACK		0x02
#define	DEF_USB_PID_NAK		0x0A
#define	DEF_USB_PID_STALL	0x0E
#define	DEF_USB_PID_DATA0	0x03
#define	DEF_USB_PID_DATA1	0x0B
#define	DEF_USB_PID_PRE		0x0C
#endif

/* ********************************************************************************************************************* */
/* ��????? */

/* ????????????? */

#define	RAM_ENDP0_SIZE		0x08		/* ???0??????????? */
#define	RAM_ENDP0_TRAN		0x20		/* ???0????????????????? */
#define	RAM_ENDP0_RECV		0x28		/* ???0????????????????? */
#define	RAM_ENDP1_SIZE		0x08		/* ???1??????????? */
#define	RAM_ENDP1_TRAN		0x30		/* ???1????????????????? */
#define	RAM_ENDP1_RECV		0x38		/* ???1????????????????? */
#define	RAM_ENDP2_SIZE		0x40		/* ???2??????????? */
#define	RAM_ENDP2_TRAN		0x40		/* ???2????????????????? */
#define	RAM_ENDP2_RECV		0xC0		/* ???2????????????????? */
#define	RAM_ENDP2_EXCH		0x80		/* ???2????????????????? */
#define	RAM_HOST_TRAN		0x40		/* ????????????????????? */
#define	RAM_HOST_RECV		0xC0		/* ????????????????????? */
#define	RAM_HOST_EXCH		0x80		/* ????????????????????? */

#define	REG_HUB_SETUP		0x02		/* ??USB???????: ROOT-HUB???? */
#define	REG_HUB_CTRL		0x03		/* ??USB???????: ROOT-HUB???? */
#define	REG_SYS_INFO		0x04		/* ?????, ??? */
#define	REG_SYS_CTRL		0x05		/* ??????, ??????????��??? */
#define	REG_USB_SETUP		0x06		/* USB???? */
#define	REG_INTER_EN		0x07		/* ?��???? */
#define	REG_USB_ADDR		0x08		/* USB?��??? */
#define	REG_INTER_FLAG		0x09		/* ?��???, ???, �˧�1??0 */
#define	REG_USB_STATUS		0x0A		/* USB??, ??? */
#define	REG_USB_LENGTH		0x0B		/* USB????, ??????USB???????, ?��?????��?USB???2, ?????????��?USB??????????? */
#define	REG_USB_ENDP0		0x0C		/* ??USB?��???: USB???0???? */
#define	REG_USB_ENDP1		0x0D		/* ??USB?��???: USB???1???? */
#define	REG_USB_H_PID		0x0D		/* ??USB???????: USB???????? */
#define	REG_USB_ENDP2		0x0E		/* ??USB?��???: USB???2???? */
#define	REG_USB_H_CTRL		0x0E		/* ??USB???????: USB???????? */

/* ???????��?????��??? */

#define	BIT_HUB0_EN			0x01		/* HUB0????USB???????: 0-???, 1-???? */
#define	BIT_HUB0_RESET		0x02		/* HUB0????USB?????��????: 0-????��, 1-??�� */
#define	BIT_HUB0_POLAR		0x04		/* HUB0?????????????: 0-??????/???, 1-??????/???? */
#define	BIT_HUB0_ATTACH		0x08		/* HUB0????USB?��??????(???): 0-??��????/???/?��?, 1-???????/???? */
#define	BIT_HUB1_DX_IN		0x10		/* HUB1??????????UD+????/??????????UD-??????????: 0-????,??????, 1-????,?????? */
#define	BIT_HUB2_DX_IN		0x20		/* HUB2??????????UD+????/??????????UD-??????????: 0-????,??????, 1-????,?????? */
#define	BIT_HUB_PRE_PID		0x40		/* ????????PRE PID???????: 0-???, 1-????(???��??USB-HUB) */
#define	BIT_HUB_DISABLE		0x80		/* ???ROOT-HUB????????????: 0-????(??USB???????), 1-???(???) */

#define	BIT_HUB1_EN			0x01		/* HUB1????USB???????: 0-???, 1-???? */
#define	BIT_HUB1_RESET		0x02		/* HUB1????USB?????��????: 0-????��, 1-??�� */
#define	BIT_HUB1_POLAR		0x04		/* HUB1?????????????: 0-??????/???, 1-??????/???? */
#define	BIT_HUB1_ATTACH		0x08		/* HUB1????USB?��??????(???): 0-??��????/???/?��?, 1-???????/???? */
#define	BIT_HUB2_EN			0x10		/* HUB2????USB???????: 0-???, 1-???? */
#define	BIT_HUB2_RESET		0x20		/* HUB2????USB?????��????: 0-????��, 1-??�� */
#define	BIT_HUB2_POLAR		0x40		/* HUB2?????????????: 0-??????/???, 1-??????/???? */
#define	BIT_HUB2_ATTACH		0x80		/* HUB2????USB?��??????(???): 0-??��????/???/?��?, 1-???????/???? */

#define	BIT_INFO_HW_ID		0x03		/* ??????��: ???????01, ?????????????????????????? */
#define	BIT_INFO_USB_DM		0x04		/* USB????HUB0??UD-?????????????: 0-????, 1-???? */
#define	BIT_INFO_USB_DP		0x08		/* USB????HUB0??UD+?????????????: 0-????, 1-???? */
#define	BIT_INFO_CLK_8KHZ	0x10		/* ???8KHz???�� */
#define	BIT_INFO_SOF_PRES	0x20		/* ???1mS?????????,????USB????, 1??????????SOF */
#define	BIT_INFO_WAKE_UP	0x40		/* ��???????: 0-???????????????, 1-????? */
#define	BIT_INFO_POWER_RST	0x80		/* ?????�y��?????: 0-?????��, 1-??��??? */

#define	BIT_CTRL_OSCIL_OFF	0x01		/* ???????????: 0-??????, 1-???? */
#define	BIT_CTRL_CLK_12MHZ	0x02		/* ?????????????: 0-24MHz, 1-12MHz */
#define	BIT_CTRL_USB_POWER	0x04		/* V3?????USB?????????????: 0-????, 1-???? */
#define	BIT_CTRL_RESET_NOW	0x08		/* ��???????��????: 0-????��, 1-??�� */
#define	BIT_CTRL_WATCH_DOG	0x10		/* RST?????RST#???????????��???: 0-????, 1-???? */
#define	BIT_CTRL_INT_PULSE	0x20		/* INT#??????��???????: 0-?????��?, 1-?????????��? */
#define	BIT_CTRL_OE_POLAR	0x40		/* UEN?????USB?????????: 0-???????, 1-??????? */

#define	BIT_SETP_TRANS_EN	0x01		/* ??USB?��???: USB?��???????: 0-???, 1-???? */
#define	BIT_SETP_PULLUP_EN	0x02		/* ??USB?��???: USB???????????: 0-????????????, 1-???????????? */
#define	BIT_SETP_BUS_CTRL	0x03		/* ??USB???????: USB??????????: 00-????/????, 01-D+??D-??(?????��), 10-????, 11-D+??D-??(??????) */
#define	M_SET_USB_BUS_FREE( old )		( (old) & ~ BIT_SETP_BUS_CTRL | 0x00 )		/* ??USB???????: USB??????? */
#define	M_SET_USB_BUS_RESET( old )		( (old) & ~ BIT_SETP_BUS_CTRL | 0x01 )		/* ??USB???????: USB??????????/D+??D-??(?????��) */
#define	M_SET_USB_BUS_RESUME( old )		( (old) & ~ BIT_SETP_BUS_CTRL | 0x03 )		/* ??USB???????: USB??????????/D+??D-??(??????) */
#define	BIT_SETP_RAM_MODE	0x0C		/* ???????????��??: 00-????????????, 01-??????????????????????128???, 10-???????????????, 11-??????????????? */
#define	M_SET_RAM_MODE_OFF( old )		( (old) & ~ BIT_SETP_RAM_MODE | 0x00 )		/* ????????????/???????????? */
#define	M_SET_RAM_MODE_128( old )		( (old) & ~ BIT_SETP_RAM_MODE | 0x04 )		/* ????????????/??????????????????????128??? */
#define	M_SET_RAM_MODE_2TX( old )		( (old) & ~ BIT_SETP_RAM_MODE | 0x08 )		/* ????????????/????????????????,??????????? */
#define	M_SET_RAM_MODE_2RX( old )		( (old) & ~ BIT_SETP_RAM_MODE | 0x0C )		/* ????????????/????????????????,??????????? */
#define	BIT_SETP_LOW_SPEED	0x20		/* USB??????????: 0-12Mbps, 1-1.5Mbps */
#define	BIT_SETP_USB_SPEED	0x30		/* USB????????: 00-?????/??????12Mbps, 11-??????/??????1.5Mbps */
#define	BIT_SETP_LED_ACT	0x40		/* ??USB?��???: ACT#???????????????: 0-??????????, 1-USB?????? */
#define	BIT_SETP_AUTO_SOF	0x40		/* ??USB???????: ???????SOF???: 0-???, 1-???? */
#define	BIT_SETP_HOST_MODE	0x80		/* USB?????????: 0-?��???, 1-??????? */

#define	BIT_IE_TRANSFER		0x01		/* USB????????��????, 1??�� */
#define	BIT_IE_BUS_RESET	0x02		/* ??USB?��???: USB?????��?��????, 1??�� */
#define	BIT_IE_DEV_DETECT	0x02		/* ??USB???????: USB?��????��????, 1??�� */
#define	BIT_IE_USB_SUSPEND	0x04		/* USB????????��????, 1??�� */
#define	BIT_IE_USB_RESUME	0x08		/* USB??????/?????��????, 1??��, 0-???��?????????��?, 1-???USB???????��? */
#define	BIT_IE_CLK_OUT_DIV	0xF0		/* ??????????????: ???????48MHz/(???+1), ????: 0000-48MHz, 0001-24MHz, 0010-16MHz, 1111-3MHz */
#define	M_SET_CLK_DIV( old, div )		( (old) & ~ BIT_IE_CLK_OUT_DIV | (div) << 4 )	/* ????????????????? */

#define	BIT_ADDR_USB_DEV	0x7F		/* ???��?????????????USB?��????, ???????????????????????USB?��??? */

#define	BIT_IF_INTER_FLAG	0x0F		/* ???��?USB?��??? */
#define	BIT_IF_TRANSFER		0x01		/* USB????????��???, 1??��, ???�˧�1????, ??��?????USB?????????????1 */
#define	BIT_IF_BUS_RESET	0x02		/* ??USB?��???: USB?????��?��???, 1??��, ???�˧�1????, ??��????USB?????��??????1 */
#define	BIT_IF_DEV_DETECT	0x02		/* ??USB???????: USB?��??��???��???, 1??��, ???�˧�1????, ??��????USB?��??��??????1 */
#define	BIT_IF_USB_SUSPEND	0x04		/* USB????????��???, 1??��, ???�˧�1????, ??��????USB?????????????1 */
#define	BIT_IF_WAKE_UP		0x08		/* ��?????????��???, 1??��, ???�˧�1????, ??��??��??????????????1 */
#define	BIT_IF_USB_RESUME	0x08		/* USB??????/?????��???, 1??��, ???�˧�1????, ??��????USB????????????1 */
#define	BIT_IF_USB_PAUSE	0x10		/* USB??????????, 1??��, ???�˧�1????, ??��?????USB?????????????1 */
#define	BIT_IF_DEV_ATTACH	0x20		/* USB?��??????: 0-??��????/???/?��?, 1-?????????USB?��???????/???? */
#define	BIT_IF_USB_OE		0x40		/* UEN?????USB????????: 0-UEN?????????, 1-UEN????????? */
#define	BIT_IF_USB_DX_IN	0x80		/* HUB0??????????UD+????/??????????UD-??????????: 0-????,??????, 1-????,?????? */
#define	BIT_HUB0_DX_IN		0x80		/* HUB0??????????UD+????/??????????UD-??????????: 0-????,??????, 1-????,?????? */

#define	BIT_STAT_THIS_ENDP	0x03		/* ??USB?��???: USB???????????: 00-???0, 01-???1, 10-???2, 11-???? */
#define	BIT_STAT_THIS_PID	0x0C		/* ??USB?��???: USB?????????/????PID: 00-OUT????, 01-????, 10-IN????, 11-SETUP???? */
#define	BIT_STAT_PID_ENDP	0x0F		/* ??USB?��???: USB?????????????,?��??????USB_INT_EP*???? */
#define	BIT_STAT_DEV_RESP	0x0F		/* ??USB???????: USB?��?????PID: XX00=??????,?????-?PID????,?��??????USB_INT_RET_*???? */
#define	M_IS_HOST_TIMEOUT( status )		( ( (status) & 0x03 ) == 0 )		/* ???USB??????????????/???? */
#define	M_IS_HOST_IN_DATA( status )		( ( (status) & BIT_STAT_DEV_RESP & ~ ( DEF_USB_PID_DATA0 ^ DEF_USB_PID_DATA1 ) ) == ( DEF_USB_PID_DATA0 & DEF_USB_PID_DATA1 ) )	/* ???????DATA0????DATA1 */
#define	BIT_STAT_TOG_MATCH	0x10		/* ???????????????/???????????????????: 0-?????, 1-??? */
#define	BIT_STAT_BUS_RESET	0x20		/* ???USB?????��??: 0-??��?��, 1-?????�� */
#define	BIT_STAT_SUSPEND	0x40		/* ???USB?????????: 0-?????��?, 1-??????? */
#define	BIT_STAT_SIE_FREE	0x80		/* ???USB???????SIE????: 0=?/???????, 1=????/??? */

#define	BIT_EP0_TRAN_RESP	0x0F		/* ??USB?��???: ???0???????: 0000~1000-??????????0~8, 1110-???NAK, 1111-???STALL,?????-???? */
#define	M_SET_EP0_TRAN_ACK( old, len )	( ((old) & (~ BIT_EP0_TRAN_RESP)) | ((len) & 0x0F) )	/* ??USB?��???: ???0???????/???ACK */
#define	M_SET_EP0_TRAN_NAK( old )		( ((old) & (~ BIT_EP0_TRAN_RESP)) | 0x0E )		/* ??USB?��???: ???0???????/???NAK */
#define	M_SET_EP0_TRAN_STA( old )		( ((old) & (~ BIT_EP0_TRAN_RESP)) | 0x0F )		/* ??USB?��???: ???0???????/???STALL */
#define	BIT_EP0_RECV_RESP	0x30		/* ??USB?��???: ???0???????: 00-???ACK, 01-????, 10-???NAK, 11-???STALL */
#define	M_SET_EP0_RECV_ACK( old )		( ((old) & (~ BIT_EP0_RECV_RESP)) | 0x00 )		/* ??USB?��???: ???0???????/???ACK */
#define	M_SET_EP0_RECV_NAK( old )		( ((old) & (~ BIT_EP0_RECV_RESP)) | 0x20 )		/* ??USB?��???: ???0???????/???NAK */
#define	M_SET_EP0_RECV_STA( old )		( ((old) & (~ BIT_EP0_RECV_RESP)) | 0x30 )		/* ??USB?��???: ???0???????/???STALL */
#define	BIT_EP0_TRAN_TOG	0x40		/* ??USB?��???: ???0??????????: 0-DATA0, 1-DATA1 */
#define	BIT_EP0_RECV_TOG	0x80		/* ??USB?��???: ???0??????????: 0-DATA0, 1-DATA1 */

#define	BIT_EP1_TRAN_RESP	0x0F		/* ??USB?��???: ???1???????: 0000~1000-??????????0~8, 1110-???NAK, 1111-???STALL,?????-???? */
#define	M_SET_EP1_TRAN_ACK( old, len )	( ((old) & (~ BIT_EP1_TRAN_RESP)) | ((len) & 0x0F) )	/* ??USB?��???: ???1???????/???ACK */
#define	M_SET_EP1_TRAN_NAK( old )		( ((old) & (~ BIT_EP1_TRAN_RESP)) | 0x0E )		/* ??USB?��???: ???1???????/???NAK */
#define	M_SET_EP1_TRAN_STA( old )		( (old) & ~ BIT_EP1_TRAN_RESP | 0x0F )		/* ??USB?��???: ???1???????/???STALL */
#define	BIT_EP1_RECV_RESP	0x30		/* ??USB?��???: ???1???????: 00-???ACK, 01-????, 10-???NAK, 11-???STALL */
#define	M_SET_EP1_RECV_ACK( old )		( ((old) & (~ BIT_EP1_RECV_RESP)) | 0x00 )		/* ??USB?��???: ???1???????/???ACK */
#define	M_SET_EP1_RECV_NAK( old )		( ((old) & (~ BIT_EP1_RECV_RESP)) | 0x20 )		/* ??USB?��???: ???1???????/???NAK */
#define	M_SET_EP1_RECV_STA( old )		( ((old) & (~ BIT_EP1_RECV_RESP)) | 0x30 )		/* ??USB?��???: ???1???????/???STALL */
#define	BIT_EP1_TRAN_TOG	0x40		/* ??USB?��???: ???1??????????: 0-DATA0, 1-DATA1 */
#define	BIT_EP1_RECV_TOG	0x80		/* ??USB?��???: ???1??????????: 0-DATA0, 1-DATA1 */

#define	BIT_HOST_PID_ENDP	0x0F		/* ??USB???????: ??????: 0000~1111-????0~15 */
#define	BIT_HOST_PID_TOKEN	0xF0		/* ??USB???????: ???????/????PID: 1101-SETUP????, 0001-OUT????, 1001-IN????, 0101-SOF??,?????-???? */
#define	M_MK_HOST_PID_ENDP( pid, endp )	( ((pid) << 4) | ((endp) & BIT_HOST_PID_ENDP) )		/* ??????/????PID????????????USB???????????? */

#define	BIT_EP2_TRAN_RESP	0x03		/* ??USB?��???: ???2???????: 00-???????, 01-???/???????, 10-???NAK, 11-???STALL */
#define	M_SET_EP2_TRAN_ACK( old )		( ((old) & (~ BIT_EP2_TRAN_RESP)) | 0x00 )		/* ??USB?��???: ???2???????/???ACK */
#define	M_SET_EP2_TRAN_ISO( old )		( ((old) & (~ BIT_EP2_TRAN_RESP)) | 0x01 )		/* ??USB?��???: ???2???????/???/???????/??????? */
#define	M_SET_EP2_TRAN_NAK( old )		( ((old) & (~ BIT_EP2_TRAN_RESP)) | 0x02 )		/* ??USB?��???: ???2???????/???NAK */
#define	M_SET_EP2_TRAN_STA( old )		( ((old) & (~ BIT_EP2_TRAN_RESP)) | 0x03 )		/* ??USB?��???: ???2???????/???STALL */
#define	BIT_EP2_RECV_RESP	0x30		/* ??USB?��???: ???2???????: 00-???ACK, 01-???/???????, 10-???NAK, 11-???STALL */
#define	M_SET_EP2_RECV_ACK( old )		( ((old) & (~ BIT_EP2_RECV_RESP)) | 0x00 )		/* ??USB?��???: ???2???????/???ACK */
#define	M_SET_EP2_RECV_ISO( old )		( ((old) & (~ BIT_EP2_RECV_RESP)) | 0x10 )		/* ??USB?��???: ???2???????/???/???????/??????? */
#define	M_SET_EP2_RECV_NAK( old )		( ((old) & (~ BIT_EP2_RECV_RESP)) | 0x20 )		/* ??USB?��???: ???2???????/???NAK */
#define	M_SET_EP2_RECV_STA( old )		( ((old) & (~ BIT_EP2_RECV_RESP)) | 0x30 )		/* ??USB?��???: ???2???????/???STALL */
#define	BIT_EP2_TRAN_TOG	0x40		/* ??USB?��???: ???2??????????: 0-DATA0, 1-DATA1 */
#define	BIT_EP2_RECV_TOG	0x80		/* ??USB?��???: ???2??????????: 0-DATA0, 1-DATA1 */

#define	BIT_HOST_TRAN_ISO	0x01		/* ??USB???????: ????????????????: 0-????/????/?��????, 1-???/??????? */
#define	BIT_HOST_START		0x08		/* ??USB???????: ????????????????: 0-???, 1-????????,?????????0 */
#define	BIT_HOST_RECV_ISO	0x10		/* ??USB???????: ????????????????: 0-????/????/?��????, 1-???/??????? */
#define	BIT_HOST_TRAN_TOG	0x40		/* ??USB???????: ??????????????: 0-DATA0, 1-DATA1 */
#define	BIT_HOST_RECV_TOG	0x80		/* ??USB???????: ??????????????: 0-DATA0, 1-DATA1 */

/* ???????????? */

#define	CMD_SPI_374READ		0xC0		/* SPI?????????? */
#define	CMD_SPI_374WRITE	0x80		/* SPI��???????? */

#define CH374_BLOCK_SIZE		64		/* CH374 maximum data block size, CH374_MAX_DATA_LEN */

/* ********************************************************************************************************************* */
/* USB?? */

/* ??????????????USB?��??? */
/*   ��4?????????????????????, 0=?????, 1-??? */
/*   ��3-��2?????????, 00=OUT, 10=IN, 11=SETUP */
/*   ��1-��0????????, 00=???0, 01=???1, 10=???2 */
#ifndef	USB_INT_EP0_SETUP
#define	USB_INT_EP0_SETUP	0x0C			/* USB???0??SETUP */
#define	USB_INT_EP0_OUT		0x00			/* USB???0??OUT */
#define	USB_INT_EP0_IN		0x08			/* USB???0??IN */
#define	USB_INT_EP1_OUT		0x01			/* USB???1??OUT */
#define	USB_INT_EP1_IN		0x09			/* USB???1??IN */
#define	USB_INT_EP2_OUT		0x02			/* USB???2??OUT */
#define	USB_INT_EP2_IN		0x0A			/* USB???2??IN */
#endif

/* ??????????????USB??????? */
/*   ��4?????????????????????, 0=?????, 1-??? */
/*   ��3-��0??USB?��?????: 0010=ACK, 1010=NAK, 1110=STALL, 0011=DATA0, 1011=DATA1, XX00=???????????????? */
#ifndef	USB_INT_RET_ACK
#define	USB_INT_RET_ACK		DEF_USB_PID_ACK		/* ????:????OUT/SETUP?????ACK */
#define	USB_INT_RET_NAK		DEF_USB_PID_NAK		/* ????:????NAK */
#define	USB_INT_RET_STALL	DEF_USB_PID_STALL	/* ????:????STALL */
#define	USB_INT_RET_DATA0	DEF_USB_PID_DATA0	/* ????:????IN?????DATA0 */
#define	USB_INT_RET_DATA1	DEF_USB_PID_DATA1	/* ????:????IN?????DATA1 */
#define	USB_INT_RET_TOUT	0x00			/* ????:???????????????? */
#define	USB_INT_RET_TOUT1	0x04			/* ????:???????????????? */
#define	USB_INT_RET_TOUT2	0x08			/* ????:???????????????? */
#define	USB_INT_RET_TOUT3	0x0C			/* ????:???????????????? */
#endif

/* ??????????1XH????USB??????????????????, ???????��????????, ???????????????CH375 */
#ifndef	USB_INT_SUCCESS
#define	USB_INT_SUCCESS		0x14			/* USB???????????????? */
#define	USB_INT_CONNECT		0x15			/* ???USB?��??????? */
#define	USB_INT_DISCONNECT	0x16			/* ???USB?��?????? */
#define	USB_INT_BUF_OVER	0x17			/* USB???????????????, ????????? */
#define	USB_INT_DISK_ERR	0x1F			/* USB?��????????? */
#endif
#ifndef	USB_INT_CONNECT_LS
#define	USB_INT_CONNECT_LS	0x13			/* ???????USB?��??????? */
#endif

/* ********************************************************************************************************************* */
/* ????USB?????????? */

typedef struct _USB_SETUP_REQ {
	uint8_t	bType;
	uint8_t	bReq;
	uint8_t	wValueL;
	uint8_t	wValueH;
	uint8_t	wIndexL;
	uint8_t	wIndexH;
	uint8_t	wLengthL;
	uint8_t	wLengthH;
} USB_SETUP_REQ, *PUSB_SETUP_REQ;

typedef struct _USB_DEVICE_DESCRIPTOR {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint8_t bcdUSBL;
    uint8_t bcdUSBH;
    uint8_t bDeviceClass;
    uint8_t bDeviceSubClass;
    uint8_t bDeviceProtocol;
    uint8_t bMaxPacketSize0;
    uint8_t idVendorL;
    uint8_t idVendorH;
    uint8_t idProductL;
    uint8_t idProductH;
    uint8_t bcdDeviceL;
    uint8_t bcdDeviceH;
    uint8_t iManufacturer;
    uint8_t iProduct;
    uint8_t iSerialNumber;
    uint8_t bNumConfigurations;
} USB_DEV_DESCR, *PUSB_DEV_DESCR;

typedef struct _USB_CONFIG_DESCRIPTOR {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint8_t wTotalLengthL;
    uint8_t wTotalLengthH;
    uint8_t bNumInterfaces;
    uint8_t bConfigurationValue;
    uint8_t iConfiguration;
    uint8_t bmAttributes;
    uint8_t MaxPower;
} USB_CFG_DESCR, *PUSB_CFG_DESCR;

typedef struct _USB_INTERF_DESCRIPTOR {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint8_t bInterfaceNumber;
    uint8_t bAlternateSetting;
    uint8_t bNumEndpoints;
    uint8_t bInterfaceClass;
    uint8_t bInterfaceSubClass;
    uint8_t bInterfaceProtocol;
    uint8_t iInterface;
} USB_ITF_DESCR, *PUSB_ITF_DESCR;

typedef struct _USB_HID_DESCRIPTOR {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint8_t bcdHIDL;
    uint8_t bcdHIDH;
    uint8_t bCountryCode;
    uint8_t bNumDescriptors;
    uint8_t bDescriptorType2;
    uint8_t bDescriptorLengthL;
    uint8_t bDescriptorLengthH;
} USB_HID_DESCR, *PUSB_HID_DESCR;

typedef struct _USB_ENDPOINT_DESCRIPTOR {
    uint8_t bLength;
    uint8_t bDescriptorType;
    uint8_t bEndpointAddress;
    uint8_t bmAttributes;
    uint8_t wMaxPacketSize;
    uint8_t wMaxPacketSize1;
    uint8_t bInterval;
} USB_ENDP_DESCR, *PUSB_ENDP_DESCR;

typedef struct _USB_CONFIG_DESCRIPTOR_LONG {
	USB_CFG_DESCR	cfg_descr;
	USB_ITF_DESCR	itf_descr;
	USB_ENDP_DESCR	endp_descr[4];
} USB_CFG_DESCR_LONG, *PUSB_CFG_DESCR_LONG;

/* USB???????? */
#ifndef	DEF_USB_REQ_TYPE
#define	DEF_USB_REQ_READ		0x80			/* ????????? */
#define	DEF_USB_REQ_WRITE		0x00			/* ????��???? */
#define	DEF_USB_REQ_TYPE		0x60			/* ???????????? */
#define	DEF_USB_REQ_STAND		0x00			/* ??????? */
#define	DEF_USB_REQ_CLASS		0x20			/* ?��?????? */
#define	DEF_USB_REQ_VENDOR		0x40			/* ???????? */
#define	DEF_USB_REQ_RESERVE		0x60			/* ???????? */
#endif

/* USB????��????, RequestType??��6��5=00(Standard) */
#ifndef	DEF_USB_GET_DESCR
#define	DEF_USB_CLR_FEATURE		0x01
#define	DEF_USB_SET_FEATURE		0x03
#define	DEF_USB_GET_STATUS		0x00
#define	DEF_USB_SET_ADDRESS		0x05
#define	DEF_USB_GET_DESCR		0x06
#define	DEF_USB_SET_DESCR		0x07
#define	DEF_USB_GET_CONFIG		0x08
#define	DEF_USB_SET_CONFIG		0x09
#define	DEF_USB_GET_INTERF		0x0A
#define	DEF_USB_SET_INTERF		0x0B
#define	DEF_USB_SYNC_FRAME		0x0C
#endif

/* ?????????? */
#ifndef	USB_DEVICE_DESCR_TYPE
#define	USB_DEVICE_DESCR_TYPE		0x01
#define	USB_CONFIG_DESCR_TYPE		0x02
#define	USB_STRING_DESCR_TYPE		0x03
#define	USB_INTERF_DESCR_TYPE		0x04
#define	USB_ENDP_DESCR_TYPE			0x05
#define	USB_POWER_DESCR_TYPE		0x06
#define	USB_HID_DESCR_TYPE			0x21
#define	USB_REPORT_DESCR_TYPE		0x22
#define	USB_PHYSIC_DESCR_TYPE		0x23
#endif

/* ???????????? */
#ifndef	USB_ENDP_TYPE_MASK
#define USB_ENDP_DIR_MASK			0x80
#define USB_ENDP_TYPE_MASK			0x03
#define USB_ENDP_TYPE_CTRL			0x00
#define USB_ENDP_TYPE_ISOCH			0x01
#define USB_ENDP_TYPE_BULK			0x02
#define	USB_ENDP_TYPE_INTER			0x03
#endif

/* ?��??? */
#ifndef	USB_DEV_CLASS_HUB
#define	USB_DEV_CLASS_RESERVED		0x00
#define	USB_DEV_CLASS_AUDIO			0x01
#define	USB_DEV_CLASS_COMMUNIC		0x02
#define	USB_DEV_CLASS_HUMAN_IF		0x03
#define	USB_DEV_CLASS_MONITOR		0x04
#define	USB_DEV_CLASS_PHYSIC_IF		0x05
#define	USB_DEV_CLASS_POWER			0x06
#define	USB_DEV_CLASS_PRINTER		0x07
#define	USB_DEV_CLASS_STORAGE		0x08
#define	USB_DEV_CLASS_HUB			0x09
#define	USB_DEV_CLASS_VEN_SPEC		0xFF
#endif

#define	DEFAULT_ENDP0_SIZE	8				/* ??????0???????? */

#endif